local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog = require "filelog"
local tabletool = require "tabletool"
require "ddzenum"

local TablesvrHelper = {}
local server

function TablesvrHelper:init(base)
    server = base
end

function TablesvrHelper:get_server()
    return server
end

function TablesvrHelper:sendmsg_to_alltableplayer(msgname, msg, ...)
    local table_data = server.table_data
    local rids = {}
    --通知座位上的玩家
    for _, seat in ipairs(table_data.seats) do
        if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and seat.rid > 100 then
            if not seat.waitreenter then
                table.insert(rids, seat.rid)
            end
        end
    end

    for _, wait in pairs(table_data.waits) do
        if wait.rid > 100 and not wait.waitreenter then
            table.insert(rids, wait.rid)
        end
    end
    cluster.send("gatenode", ".router", "notify", rids, "ddz." .. msgname, msg, ...)
end

function TablesvrHelper:sendmsg_to_tableplayer(seat, msgname, ...)
    if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and seat.rid > 100 then
        if not seat.waitreenter then
            cluster.send("gatenode", ".router", "notify", {seat.rid}, "ddz." .. msgname, ...)
        end
    end
end

function TablesvrHelper:sendmsg_to_allwaitplayer(msgname, msg, ...)
    local table_data = server.table_data
    local rids = {}
    for rid, wait in pairs(table_data.waits) do
        if wait.rid > 100 then
            table.insert(rids, wait.rid)
        end
    end
    cluster.send("gatenode", ".router", "notify", rids, "ddz." .. msgname, ...)
end

function TablesvrHelper:sendmsg_to_waitplayer(wait, msgname, ...)
    cluster.send("gatenode", ".router", "notify", {wait.rid}, "ddz." .. msgname, ...)
end

function TablesvrHelper:sendmsg_to_roomsvr(msgname, ...)
    skynet.send(".router", "lua", msgname, ...)
end

function TablesvrHelper:callmsg_to_roomsvr(msgname, ...)
    return skynet.call(".router", "lua", msgname, ...)
end

function TablesvrHelper:sendmsg_to_datasvr(msgname, ...)
    cluster.send("datanode", ".router", msgname, ...)
end

function TablesvrHelper:sendmsg_to_gatesvr(msgname, ...)
    cluster.send("gatenode", ".router", msgname, ...)
end

function TablesvrHelper:callmsg_to_gatesvr(msgname, ...)
    local stat, ret = pcall(cluster.call, "gatenode", ".router", msgname, ...)
    if not stat then return false end
    return ret
end

function TablesvrHelper:copy_table_gameinfo(gameinfo)
    local table_data = server.table_data
    gameinfo.idx = table_data.idx
    gameinfo.state = table_data.state
    gameinfo.name = table_data.conf.name
    gameinfo.room_type = table_data.conf.room_type
    gameinfo.game_type = table_data.conf.game_type
    gameinfo.max_player_num = table_data.conf.max_player_num
    gameinfo.cur_player_num = table_data.sitdown_player_num
    -- gameinfo.game_time = table_data.conf.game_time
    gameinfo.create_user_rid = table_data.conf.create_user_rid
    gameinfo.create_user_rolename = table_data.conf.create_user_rolename
    gameinfo.create_time = table_data.conf.create_time
    gameinfo.table_code = table_data.conf.table_code
    gameinfo.action_timeout = table_data.conf.action_timeout
    gameinfo.action_timeout_count = table_data.conf.action_timeout_count           
    gameinfo.create_user_logo = table_data.conf.create_user_logo
    gameinfo.min_carry_diamond = table_data.conf.min_carry_diamond
    gameinfo.leave_carry_diamond = table_data.conf.leave_carry_diamond
    gameinfo.base_score = table_data.conf.base_score
    gameinfo.max_score = table_data.conf.max_score
    gameinfo.common_times  = table_data.conf.common_times
    gameinfo.all_times = table_data.basetimes

    gameinfo.action_seat_index = table_data.action_seat_index
    gameinfo.action_to_time = table_data.action_to_time
    gameinfo.action_type = table_data.action_type

    gameinfo.cur_game_times = table_data.cur_games_times or 0
    gameinfo.total_times = table_data.conf.game_times or 0
    gameinfo.the_round = table_data.the_round
    gameinfo.lastcards = table_data.lastcards
    gameinfo.lzcards = table_data.lztab
    gameinfo.dz_index = table_data.dz_seat_index

    gameinfo.seats = {}
    gameinfo.player_infos = {}
    local seatinfo, playerinfo
    for index, seat in pairs(table_data.seats) do
        seatinfo = {}
        playerinfo = {}
        self:copy_seatinfo(seatinfo, seat)
        if table_data.vote_answers then -- 是否有解散房间投票
            seatinfo.vote = table_data.vote_answers[seat.index] or -1
        else
            seatinfo.vote = -1
        end
        table.insert(gameinfo.seats, seatinfo)
        self:copy_playerinfo(playerinfo, seat)
        table.insert(gameinfo.player_infos, playerinfo)
    end

    -- 解散房间投票相关
    if table_data.vote_timerid then
        gameinfo.vote_to_time = table_data.vote_timeout
        gameinfo.vote_rid = table_data.vote_rid
		gameinfo.vote_name = table_data.vote_name
        -- gameinfo.vote_names = table_data.vote_names
        -- local agree = 0
        -- for _ in pairs(request.vote_answers or {}) do
        --     agree = agree + 1
        -- end
        -- gameinfo.vote_agree_num = agree
        gameinfo.player_disband_item = {}
        for _, _seat in ipairs(table_data.seats) do
            if _seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and _seat.rid > 0 then
                table.insert(gameinfo.player_disband_item, {
                    rid = _seat.rid,
                    rolename = _seat.playerinfo.rolename,
                    is_agree = table_data.vote_answers[_seat.index],
                })
            end
        end
    end
    gameinfo.is_public = table_data.conf.is_public
    gameinfo.tip = table_data.tip or 0
    gameinfo.curr_round_code = table_data.conf.curr_round_code
end

function TablesvrHelper:copy_seatinfo(seatinfo, seat)
    seatinfo.rid = seat.rid
    seatinfo.index = seat.index
    seatinfo.state = seat.state
    seatinfo.is_tuoguan = seat.is_tuoguan
    seatinfo.diamond = seat.diamond
    seatinfo.is_ready = seat.is_ready
    seatinfo.ready_to_time = seat.ready_to_time
    seatinfo.doubled = seat.doubled
    if seat.cards == nil then
        seatinfo.cardsnum = 0
    else
        seatinfo.cardsnum = #seat.cards
    end
    local table_data = server.table_data
    if table_data.vote_answers then -- 是否有解散房间投票
        seatinfo.vote = table_data.vote_answers[seat.index] or -1
    else
        seatinfo.vote = -1
    end
end

function TablesvrHelper:copy_playerinfo(playerinfo, seat)
    playerinfo.rid = seat.rid
    playerinfo.rolename = seat.playerinfo.rolename
    playerinfo.logo = seat.playerinfo.logo or ''
    playerinfo.sex = seat.playerinfo.sex or 0
    playerinfo.ip_addr = seat.playerinfo.ip_addr or ''
    playerinfo.fangka = seat.playerinfo.fangka or 0
    playerinfo.diamond = seat.playerinfo.diamond or 0
    playerinfo.coin = seat.playerinfo.coin or 0
    playerinfo.introduce = seat.playerinfo.introduce or ''
    playerinfo.upvoted_num = seat.playerinfo.upvoted_num
    playerinfo.downvoted_num = seat.playerinfo.downvoted_num
    playerinfo.gps_addr = seat.playerinfo.gps_addr
    playerinfo.logoframe = seat.playerinfo.logoframe or 100
end

function TablesvrHelper:copy_playerinfoingameend(playerendinfos)
    local table_data = server.table_data
    local playerendinfo
    local ischuntian = true
    for index, seat in pairs(table_data.seats) do
        if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and seat.rid > 0 then
            playerendinfo = {}
            playerendinfo.rid = seat.rid
            playerendinfo.rolename = seat.playerinfo.rolename --玩家名字
            playerendinfo.allcoins = seat.diamond --当前玩家总金币
            if table_data.conf.room_type == ERoomType.ROOM_TYPE_COMMON then 
                playerendinfo.getallcoins = table_data.tmpdiamond[seat.rid].value
                playerendinfo.getdiamonds = table_data.tmpdiamond[seat.rid].value
                playerendinfo.getcoins = 0
            elseif table_data.conf.room_type == ERoomType.ROOM_TYPE_COIN then
                playerendinfo.getallcoins = table_data.tmpdiamond.coin[seat.rid].value
                playerendinfo.getcoins = table_data.tmpdiamond.coin[seat.rid].value
                if table_data.tmpdiamond.diamond and table_data.tmpdiamond.diamond[seat.rid] then
                    playerendinfo.getallcoins = playerendinfo.getallcoins + table_data.tmpdiamond.diamond[seat.rid].value
                    playerendinfo.getdiamonds = table_data.tmpdiamond.diamond[seat.rid].value 
                end
            else
                playerendinfo.getallcoins = seat.diamond - (seat.diamond_base or 0)
                playerendinfo.getdiamonds = seat.diamond - (seat.diamond_base or 0)
                playerendinfo.getcoins = 0
            end

            playerendinfo.iswin = seat.win ----是否胜利(1胜利，0失败)
            playerendinfo.seatindex = seat.index
            playerendinfo.roomtype = table_data.conf.room_type
            playerendinfo.leftcards = tabletool.deepcopy(seat.cards)
            playerendinfo.handcards = tabletool.deepcopy(seat.handcards)
            if seat.index == table_data.dz_seat_index then
                for _, v in ipairs(table_data.lastcards) do
                    table.insert(playerendinfo.handcards, v)
                end
            end

            if table_data.bomb and table_data.bomb[seat.rid] then
                playerendinfo.bomb = table_data.bomb[seat.rid] or 0
            end
            playerendinfo.doubled = seat.doubled
            playerendinfo.jdztag = seat.jdztag
            playerendinfo.isdz = seat.isdz
            table.insert(playerendinfos,playerendinfo)
        end
    end
end

return  TablesvrHelper